package Zoo.handler;


import Zoo.entity.ZooparkEntity;
import Zoo.impl.LeaderServiceImpl;
import Zoo.service.LeaderService;
import Zoo.view.leaderview.AddZooparkView;
import Zoo.view.leaderview.LeaderView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 园长增加园区界面处理类
 */
public class AddZooparkViewHandler implements ActionListener {
    private AddZooparkView addZooparkView;
    private LeaderView leaderView;

    public AddZooparkViewHandler(AddZooparkView addZooparkView, LeaderView leaderView) {
        this.addZooparkView = addZooparkView;
        this.leaderView = leaderView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("添加".equals(text)) {

            LeaderService leaderService = new LeaderServiceImpl();
            ZooparkEntity zooparkEntity = addZooparkView.buildZooparkEntity();
            boolean addResult = leaderService.add(zooparkEntity);
            if (addResult) {
                //重新加载表格查到最新数据
                leaderView.reloadTable();
                addZooparkView.dispose();
            } else {
                JOptionPane.showMessageDialog(addZooparkView, "添加失败");
            }
        }
    }

}
